import { createApp } from "vue";

import App from "./App.vue";
import router from "./router";
import "./assets/less/index.less";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import store from "./store/index.js";
import "./api/mock.js";
import api from "./api/api"; //引入api
import axios from "./api/request";

const app = createApp(App);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
app.config.globalProperties.$api = api;
store.commit('addMenu', router); //把路由写到这里

function checkRouter(path){
 let hasCheck=router.getRoutes().filter(route=>route.path==path).length;
 if(hasCheck){
  return true;
 }else{
  return false;
 }
};

router.beforeEach((to,from,next)=>{
  store.commit('getToken');
  const token=store.state.token;
  if(!token&&to.name!='login'){
     next({name:'login'})
  }else if(!checkRouter(to.path)){
    next({name:'login'})
  }
  else{
    next();
  }
});

app.use(router);
app.use(store);



//app.config.globalProperties.$axios = axios;
//app.use(ElementPlus);
app.mount("#app");
